using BarcodePrinting.TSC;
using OrderSystem.SystemFramework;
using System;
using System.Drawing;

namespace BarcodePrinting
{
	public class Printer_BiaoZhun_Tsc : BarcodePrinter
	{
		private string _sBarcodePrinterName = "";

		private float _fPrintSpeed = 3f;

		private int _nDarkness = 8;

		private PrinterSensorType _sensor = PrinterSensorType.Vertical;

		private string _sDirection = "0";

		private string _sMirror = "0";

		private int _nPointOfmm = 8;

		private int _nLabelWidth = 50;

		private int _nLabelHeight = 30;

		private int _nColumnCountOfPage = 2;

		private int _nHorizontalGap = 2;

		private int _nVerticalGap = 2;

		private string _sPrintLogo = "0";

		private int _nOffsetX = 0;

		private int _nOffsetY = 0;

		private int _nFontSize = 0;

		private Bitmap _bmp;

		private Graphics _grfx;

		private bool _isDisposed = false;

		public Printer_BiaoZhun_Tsc()
		{
			this._sBarcodePrinterName = AppSettings.GetAppSettings("barcodePrinterName");
			this._fPrintSpeed = Convert.ToSingle(AppSettings.GetAppSettings("barcodePrinterSpeed"));
			this._nDarkness = Convert.ToInt32(AppSettings.GetAppSettings("barcodePrinterDensity"));
			this._sensor = PrinterSensorType.Vertical;
			string sSensor = AppSettings.GetAppSettings("barcodePrinterSensor");
			if (sSensor == "1")
			{
				this._sensor = PrinterSensorType.BlackMark;
			}
			if (sSensor == "2")
			{
				this._sensor = PrinterSensorType.Continuous;
			}
			this._sDirection = AppSettings.GetAppSettings("direction");
			if (this._sDirection == "1")
			{
				this._sDirection = "1";
			}
			else
			{
				this._sDirection = "0";
			}
			this._sMirror = AppSettings.GetAppSettings("mirror");
			if (this._sMirror == "1")
			{
				this._sMirror = "1";
			}
			else
			{
				this._sMirror = "0";
			}
			this._nPointOfmm = 8;
			this._nLabelWidth = 50;
			this._nLabelHeight = 30;
			this._nColumnCountOfPage = 2;
			this._nHorizontalGap = 2;
			this._nVerticalGap = 2;
			this._sPrintLogo = AppSettings.GetAppSettings("printLogo");
			this._nOffsetX = Convert.ToInt32((Convert.ToSingle(AppSettings.GetAppSettings("OffsetX")) * (float)this._nPointOfmm).ToString("F0"));
			this._nOffsetY = Convert.ToInt32((Convert.ToSingle(AppSettings.GetAppSettings("OffsetY")) * (float)this._nPointOfmm).ToString("F0"));
			this._nFontSize = Convert.ToInt32(AppSettings.GetAppSettings("FontSize"));
			this._bmp = new Bitmap(1, 1);
			this._grfx = Graphics.FromImage(this._bmp);
		}

		public override void Print(LabelModel[] labels)
		{
			TscDll.openport(this._sBarcodePrinterName);
			string arg_73_0 = (this._nLabelWidth * this._nColumnCountOfPage + this._nHorizontalGap * (this._nColumnCountOfPage - 1)).ToString();
			string arg_73_1 = this._nLabelHeight.ToString();
			string arg_73_2 = this._fPrintSpeed.ToString();
			string arg_73_3 = this._nDarkness.ToString();
			int sensor = (int)this._sensor;
			TscDll.setup(arg_73_0, arg_73_1, arg_73_2, arg_73_3, sensor.ToString(), this._nVerticalGap.ToString(), "0");
			int nColumnIndex = 1;
			for (int i = 0; i < labels.Length; i++)
			{
				int nLabelsOfItem = labels[i].PrintQuantity * labels[i].PrintMultiple;
				for (int j = 0; j < nLabelsOfItem; j++)
				{
					if (nColumnIndex == 1)
					{
						TscDll.clearbuffer();
					}
					if (this._sPrintLogo == "1")
					{
						TscDll.windowsfont(90 + (nColumnIndex - 1) * 416, this._nOffsetY, 22, 0, 0, 0, "宋体", "玻璃");
					}
					string sData = ((labels[i].LabelName == "") ? labels[i].ProjectName : labels[i].LabelName) + " " + labels[i].ProductID.Substring(4);
					Font f = new Font("黑体", (float)(36 + this._nFontSize), FontStyle.Regular, GraphicsUnit.Pixel);
					int nStringWidth = 0;
					f = this.CalculateFitFont(sData, (this._nLabelWidth - 2) * this._nPointOfmm, f, ref nStringWidth);
					TscDll.windowsfont(8 + (this._nLabelWidth + this._nHorizontalGap) * (nColumnIndex - 1) * this._nPointOfmm + this._nOffsetX, this._nOffsetY, (int)f.Size, 0, 0, 0, f.Name, sData);
					sData = labels[i].ProductType;
					f.Dispose();
					f = new Font("黑体", (float)(38 + this._nFontSize), FontStyle.Regular, GraphicsUnit.Pixel);
					nStringWidth = 0;
					f = this.CalculateFitFont(sData, (this._nLabelWidth - 2) * this._nPointOfmm, f, ref nStringWidth);
					TscDll.windowsfont(8 + (this._nLabelWidth + this._nHorizontalGap) * (nColumnIndex - 1) * this._nPointOfmm + this._nOffsetX, 38 + this._nOffsetY, (int)f.Size, 0, 0, 0, f.Name, sData);
					sData = labels[i].ProductCode;
					f.Dispose();
					f = new Font("宋体", (float)(36 + this._nFontSize), FontStyle.Regular, GraphicsUnit.Pixel);
					nStringWidth = 0;
					f = this.CalculateFitFont(sData, (this._nLabelWidth - 2) * this._nPointOfmm, f, ref nStringWidth);
					TscDll.windowsfont(8 + (this._nLabelWidth + this._nHorizontalGap) * (nColumnIndex - 1) * this._nPointOfmm + this._nOffsetX, 76 + this._nOffsetY, (int)f.Size, 0, 0, 0, f.Name, sData);
					sData = string.Concat(new string[]
					{
						labels[i].Width.ToString(),
						(labels[i].SubtenseOfWidth == "") ? "" : ("/" + labels[i].SubtenseOfWidth),
						"×",
						labels[i].Hight.ToString(),
						(labels[i].SubtenseOfHight == "") ? "" : ("/" + labels[i].SubtenseOfHight),
						" =",
						labels[i].Quantity.ToString()
					});
					f.Dispose();
					f = new Font("Times New Roman", (float)(55 + this._nFontSize), FontStyle.Bold, GraphicsUnit.Pixel);
					nStringWidth = 0;
					f = this.CalculateFitFont(sData, (this._nLabelWidth - 2) * this._nPointOfmm, f, ref nStringWidth);
					TscDll.windowsfont(8 + (this._nLabelWidth + this._nHorizontalGap) * (nColumnIndex - 1) * this._nPointOfmm + this._nOffsetX, 106 + this._nOffsetY, (int)f.Size, 0, 2, 0, f.Name, sData);
					sData = labels[i].Remark + ((labels[i].Technics != "") ? ("/" + labels[i].Technics) : "");
					f.Dispose();
					f = new Font("宋体", (float)(32 + this._nFontSize), FontStyle.Regular, GraphicsUnit.Pixel);
					nStringWidth = 0;
					f = this.CalculateFitFont(sData, (this._nLabelWidth - 2) * this._nPointOfmm, f, ref nStringWidth);
					TscDll.windowsfont(8 + (this._nLabelWidth + this._nHorizontalGap) * (nColumnIndex - 1) * this._nPointOfmm + this._nOffsetX, 158 + this._nOffsetY, (int)f.Size, 0, 0, 0, f.Name, sData);
					f.Dispose();
					if ((i == labels.Length - 1 && j == nLabelsOfItem - 1) || nColumnIndex == this._nColumnCountOfPage)
					{
						TscDll.printlabel("1", "1");
					}
					nColumnIndex++;
					if (nColumnIndex > this._nColumnCountOfPage)
					{
						nColumnIndex = 1;
					}
				}
			}
			TscDll.clearbuffer();
			TscDll.closeport();
		}

		private SizeF MeasureString(string s, Font f)
		{
			this._grfx.PageUnit = GraphicsUnit.Pixel;
			return this._grfx.MeasureString(s, f);
		}

		private Font CalculateFitFont(string s, int nPrintAreaWidth, Font f, ref int nStringWidth)
		{
			Font resultFont = null;
			SizeF sizefPrintData = SizeF.Empty;
			for (int i = (int)f.Size; i > 0; i--)
			{
				resultFont = new Font(f.Name, (float)i, f.Style, f.Unit);
				sizefPrintData = this.MeasureString(s, resultFont);
				if ((int)sizefPrintData.Width <= nPrintAreaWidth)
				{
					nStringWidth = (int)sizefPrintData.Width;
					f.Dispose();
					break;
				}
				resultFont.Dispose();
			}
			return resultFont;
		}

		public override void Dispose()
		{
			this.Dispose(true);
			GC.SuppressFinalize(this);
			base.Dispose();
		}

		protected virtual void Dispose(bool disposing)
		{
			if (!this._isDisposed)
			{
				if (disposing)
				{
					if (this._bmp != null)
					{
						this._bmp.Dispose();
						this._bmp = null;
					}
					if (this._grfx != null)
					{
						this._grfx.Dispose();
						this._grfx = null;
					}
				}
				this._isDisposed = true;
			}
		}

		~Printer_BiaoZhun_Tsc()
		{
			this.Dispose(false);
		}
	}
}
